package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.market.domain.Unit;
import com.ruoyi.market.service.IUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 计量单位信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/unit")
public class UnitController extends BaseController
{
    @Autowired
    private IUnitService unitService;

    /**
     * 获取计量单位列表
     */
    @PostMapping("/unitList")
    public TableDataInfo list(@RequestBody Unit unit, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            unit.setShopId(shopId);
        }
        startPageByPost();
        List<Unit> list = unitService.selectUnitList(unit);
        return getDataTable(list);
    }

    /**
     * 根据计量单位编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:unit:query')")
    @GetMapping(value = { "/", "/{unitId}" })
    public AjaxResult getInfo(@PathVariable(value = "unitId", required = false) Long unitId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(unitId))
        {
            Unit sysUnit = unitService.selectUnitById(unitId);
            ajax.put(AjaxResult.DATA_TAG, sysUnit);
        }
        return ajax;
    }

    /**
     * 新增计量单位
     */
    @PreAuthorize("@ss.hasPermi('market:unit:add')")
    @Log(title = "计量单位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Unit unit, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            unit.setShopId(shopId);
        }
        if (!unitService.checkUnitTitleUnique(unit))
        {
            return error("新增'" + unit.getUnitTitle() + "'失败，已存在相同数据");
        }
        unit.setCreateBy(getUsername());
        return toAjax(unitService.insertUnit(unit));
    }

    /**
     * 修改计量单位
     */
    @PreAuthorize("@ss.hasPermi('market:unit:edit')")
    @Log(title = "计量单位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Unit unit)
    {
        if (!unitService.checkUnitTitleUnique(unit))
        {
            return error("修改'" + unit.getUnitTitle() + "'失败，已存在相同数据");
        }
        unit.setUpdateBy(getUsername());
        return toAjax(unitService.updateUnit(unit));
    }

    /**
     * 删除计量单位
     */
    @PreAuthorize("@ss.hasPermi('market:unit:remove')")
    @Log(title = "计量单位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        return toAjax(unitService.deleteUnitByIds(unitIds));
    }
}
